import os
import json
import platform
from .spider import spider
from .download import down

DefOpt1 = [
    'DwVideoType',
    'SpBaseSite',
    'DwBaseSite',
    'DefDict'
]
DefOpt2 = [
    [
        'CPV',
        'CR',
        'SPV',
        'CG'
    ],
    'https://space.bilibili.com/401742377/video',
    'https://www.bilibili.com/video/',
    {
        'char': {
            '菲谢尔': 'Fischl',
            '七七': 'Qiqi',
            '刻晴': 'Keqing',
            '莫娜': 'Mona',
            '温迪': 'Venti',
            '可莉': 'Klee',
            '迪奥娜': 'Diona',
            '达达利亚': 'Tartaglia',
            '辛焱': 'Xinyan',
            '钟离': 'Zhongli',
            '阿贝多': 'Albedo',
            '甘雨': 'Ganyu',
            '魈': 'Xiao',
            '胡桃': 'Hutao',
            '罗莎莉亚': 'Rozalia',
            '烟绯': 'Yanfei',
            '优菈': 'Eula',
            '枫原万叶': 'Kaede.Kazuha',
            '神里绫华': 'Kami.Ayaka',
            '早柚': 'Sayu',
            '宵宫': 'Yoimiya',
            '埃洛伊': 'Aloy',
            '九条裟罗': 'Kujo.Sara',
            '雷电将军': 'Rai.Shogun',
            '珊瑚宫心海': 'San.Kokomi',
            '托马': 'Tohma'
        },
        'sto_cg': {
            '微风与少年': 'Breeze.Teen',
            '珍珠之歌': 'Herm.Pearl',
            '护法仙众夜叉录': 'Guard.Yakasha',
            '汇成明宵': 'Col.Mingxiao',
            '万花成风': 'Wind.Blume',
            '创龙点睛': 'Eye.Dragon',
            '追逐霆光的孤旅': 'After.Thunder',
            '许以臣民之梦': 'Dream.Civis',
            '人间至味': 'Taste.Most'
        }
    }
]
DefaultOpt = dict(zip(DefOpt1, DefOpt2))


def get_options():
    if os.path.exists('Options.json'):
        optf = open('Options.json', 'r', encoding='utf-8')
    else:
        optf = open('Options.json', 'w+', encoding='utf-8')
        json.dump(DefaultOpt, optf, ensure_ascii=False, indent=4)
        optf.close()
        optf = open('Options.json', 'r', encoding='utf-8')
    print('读取配置成功！')
    opt = json.load(optf)
    optf.close()
    return opt


def set_options(opt):
    optf = open('Options.json', 'w+', encoding='utf-8')
    json.dump(opt, optf, ensure_ascii=False, indent=4)
    print('写入配置成功！')
    optf.close()


def mainmenu():
    print('欢迎使用Genshin-bili!')
    print('请输入需要做的事：')
    print('1.爬取视频列表（需要安装Chrome和ChromeDriver)')
    print('2.下载视频')
    print('3.退出')
    a = int(input())
    return a
    pass


def main():
    opt = get_options()
    while True:
        act = mainmenu()
        if act == 1:
            spider(opt)
            if platform.system() == 'Windows':
                os.system('cls')
        elif act == 2:
            down(opt)
            if platform.system() == 'Windows':
                os.system('cls')
        elif act == 3:
            break
